@pyai/sdk 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLI.schema.json +2 -1
- package/README.md +39 -9
- package/dist/cli-omni-template.d.ts +2 -2
- package/dist/cli-omni-template.js +2 -2
- package/dist/cli.js +2 -2
- package/dist/dialer/appServer.d.ts +2 -0
- package/dist/dialer/appServer.js +19 -2
- package/dist/dialer/dialerElement.js +37 -7
- package/dist/dialer/dialingQueue.d.ts +7 -2
- package/dist/dialer/dialingQueue.js +49 -15
- package/dist/dialer/indexedDbJournal.js +19 -0
- package/dist/dialer/managedDialer.d.ts +26 -1
- package/dist/dialer/managedDialer.js +119 -8
- package/dist/dialer/serverBroker.js +19 -2
- package/dist/index.d.ts +43 -3
- package/dist/index.js +6 -0
- package/package.json +1 -1
- package/src/cli-omni-template.ts +2 -2
- package/src/cli.ts +2 -2
- package/src/dialer/app/app.html +2 -2
- package/src/dialer/app/app.js +8 -6
- package/src/dialer/appServer.ts +18 -2
- package/src/dialer/dialerElement.ts +32 -6
- package/src/dialer/dialingQueue.ts +43 -13
- package/src/dialer/indexedDbJournal.ts +15 -0
- package/src/dialer/managedDialer.ts +102 -7
- package/src/dialer/serverBroker.ts +17 -2
- package/src/index.ts +27 -3
package/CLI.schema.json
CHANGED
package/README.md
CHANGED
|
@@ -106,12 +106,12 @@ the instant the socket opens and routes server frames to typed callbacks, so you
|
|
|
106
106
|
is acked but silently dropped, giving you a connected session with zero turns):
|
|
107
107
|
|
|
108
108
|
<!-- omni-starter:start -->
|
|
109
|
-
Requires SDK 0.
|
|
109
|
+
Requires SDK 0.13.0 for `--template omni`. The runnable starter uses ESM,
|
|
110
110
|
Node 20.19+ with an explicit `ws` transport, or Node 22+. MCP 0.5.1
|
|
111
111
|
requires Node 22+. The SDK's REST client continues to support Node 18+.
|
|
112
112
|
|
|
113
113
|
```sh
|
|
114
|
-
npm install @pyai/sdk@0.
|
|
114
|
+
npm install @pyai/sdk@0.13.0
|
|
115
115
|
npx pyai init voice-demo --template omni
|
|
116
116
|
cd voice-demo
|
|
117
117
|
npm install
|
|
@@ -596,11 +596,41 @@ Bind `<pyai-dialer>` to the controller and a source/contact selector; the elemen
|
|
|
596
596
|
accepts a brand name and accent color. Tokens stay in memory. Unknown call setup
|
|
597
597
|
or stop outcomes retain the original intent and prevent another call.
|
|
598
598
|
|
|
599
|
-
`DialingQueue` provides
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
599
|
+
`DialingQueue` provides auto, priority-based dynamic, and parallel queues.
|
|
600
|
+
Use `queue.start(contacts, "parallel", { parallelLines: 2 })` for groups of two
|
|
601
|
+
to four distinct contacts. Each contact must share the same source, user and
|
|
602
|
+
calling settings. The first answered line connects and other lines are canceled.
|
|
603
|
+
All destinations need their own permission and consent; actual phone-leg durations
|
|
604
|
+
are metered separately from browser time. A lost response retains the original
|
|
605
|
+
group. Every group needs authoritative terminal evidence and explicit wrap-up
|
|
606
|
+
before the next starts. Canceled lines are attempted once and are not automatically
|
|
607
|
+
redialed. A final single contact uses normal single-line calling.
|
|
608
|
+
|
|
609
|
+
The standalone app exposes two-line parallel calling in the calling-order menu.
|
|
610
|
+
Signed native installation and OS-wide meeting capture are separate features.
|
|
611
|
+
See `softphone-desktop/README.md` for host lifecycle integration and current limits.
|
|
612
|
+
|
|
613
|
+
### Warm and cold transfers
|
|
614
|
+
|
|
615
|
+
Where your PyAI deployment enables transfers, start a single-destination call
|
|
616
|
+
with `transfer_enabled: true`. The server broker exposes optional
|
|
617
|
+
`transfers.create/complete/cancel` methods; retain normal authentication and
|
|
618
|
+
ownership checks when proxying them. The widget then shows **Consult first**
|
|
619
|
+
for warm transfer and **Transfer now** for cold transfer. Warm consultation
|
|
620
|
+
holds the caller; complete only after the target joins, or return to the caller.
|
|
621
|
+
Cold transfer leaves before the target answers.
|
|
622
|
+
|
|
623
|
+
The controller persists the target, retry key and completion/cancellation
|
|
624
|
+
command before sending them. **Recover transfer** reuses that exact intent.
|
|
625
|
+
After successful handoff, **Done** clears only the local wrap-up; the caller's
|
|
626
|
+
conversation, recording and usage continue under the original PyAI call. Closing
|
|
627
|
+
the app after a handoff does not hang up that continuing conversation. A cold
|
|
628
|
+
transfer still connecting cannot be wrapped up as a successful handoff.
|
|
629
|
+
|
|
630
|
+
Open the standalone app with `pyai dialer --transfers` and select **Allow
|
|
631
|
+
transfers** before dialing. This is an operator opt-in; the API independently
|
|
632
|
+
enforces activation and current destination rules. Parallel groups cannot opt
|
|
633
|
+
into transfers. Enabling the local control does not enable the backend feature.
|
|
604
634
|
|
|
605
635
|
### Meeting recording
|
|
606
636
|
|
|
@@ -638,7 +668,7 @@ meeting. This web component is not a signed native desktop installer.
|
|
|
638
668
|
### Download and open PyAI Phone
|
|
639
669
|
|
|
640
670
|
```sh
|
|
641
|
-
npm install -g @pyai/sdk@0.
|
|
671
|
+
npm install -g @pyai/sdk@0.13.0
|
|
642
672
|
pyai login
|
|
643
673
|
pyai dialer
|
|
644
674
|
```
|
|
@@ -664,7 +694,7 @@ journal. The app does not dial, buy a number or request a microphone at startup.
|
|
|
664
694
|
|
|
665
695
|
Manual calling and list/priority queues require a foreground start. Each next
|
|
666
696
|
call waits for confirmed completion and your Done action. Pause stops the queue
|
|
667
|
-
from advancing.
|
|
697
|
+
from advancing. Inbound human ringing is not enabled in
|
|
668
698
|
this release. A window closure submits an owned stop; reopen the same profile
|
|
669
699
|
to reconcile a completion that could not be confirmed. Workspace admission,
|
|
670
700
|
consent, recording and billing policy remain authoritative for every call.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare const omniTemplate: Readonly<{
|
|
2
|
-
"README.md": "# Runnable Omni starter\n\nRequires Node 20.19+ and ESM. Node 20 uses the included `ws` transport;\nNode 22 also works. The separate MCP server requires Node 22+.\nThis server-side example uses a synthetic read-only office-hours tool. Read\n`PYAI.md` when generating the project through the CLI.\n\n<!-- omni-install:start -->\n```sh\nnpm install @pyai/sdk@0.
|
|
3
|
-
"package.json": "{\n \"name\": \"pyai-omni-starter\",\n \"version\": \"0.0.0\",\n \"private\": true,\n \"type\": \"module\",\n \"engines\": {\n \"node\": \">=20.19.0\"\n },\n \"scripts\": {\n \"start\": \"node main.mjs\"\n },\n \"dependencies\": {\n \"@pyai/sdk\": \"0.
|
|
2
|
+
"README.md": "# Runnable Omni starter\n\nRequires Node 20.19+ and ESM. Node 20 uses the included `ws` transport;\nNode 22 also works. The separate MCP server requires Node 22+.\nThis server-side example uses a synthetic read-only office-hours tool. Read\n`PYAI.md` when generating the project through the CLI.\n\n<!-- omni-install:start -->\n```sh\nnpm install @pyai/sdk@0.13.0\nnpx pyai init voice-demo --template omni\ncd voice-demo\nnpm install\n```\n\nInitialization is offline; installing dependencies and running `main.mjs` are\nseparate steps. For an existing checkout of this example, run `npm install`\nin its directory instead.\n<!-- omni-install:end -->\n\nInject `PYAI_API_KEY` through your secret manager or shell environment. Do not\npaste a real key into source, a prompt, or shell history. `.env.example` documents\nthe variable; this program does not load `.env` or saved CLI profiles.\n\nPrepare a 24 kHz PCM16 mono WAV, at most 20 seconds, saying “Please look up the\noffice opening time.” Then run:\n\n```sh\nnode main.mjs caller.wav\n```\n\nRunning the program explicitly opens one bounded Omni session and uploads its\ncaptured reply to Hear. Both consume your key's allowed usage. Automatic retries\nare disabled. No managed number is bought and no phone call is placed.\n\n`session.mjs` waits for configuration and the greeting to drain, sends exactly\none paced input stream (caller PCM or silence), returns the read tool's result,\nand captures output using the rate from `hello.audio_out`. Never run a separate\nsilence timer alongside caller audio. `timing.mjs` is generated from the existing\nOmni evaluation harness's timing utilities.\n\nThe new private `omni-run-*` folder contains `session.wav`, `reply.wav`, and\n`report.json`. The report separates received audio, an office-hours answer\nrecovered by Hear from captured audio, and physical playback (not tested).\nThe office-hours check accepts formatting such as `opens@9a.m`, `9 a.m.` and\n`nine in the morning` in a complete affirmative opening-time sentence. Wrong\ntimes, negation, conflicting or truncated text remain unverified. This is a\nconservative check for the example fact, not a general answer-quality score.\nA synthesis transcript is advisory; it is not evidence that speech arrived.\nThis starter does not negotiate playback receipts. Capture ends after the\nsimulated queue drains and two seconds of quiet, a bounded capture heuristic.\nListen to the saved file when qualifying actual sound quality or completeness.\n\nTo exercise interruption, supply a second WAV saying “Stop speaking now”:\n\n```sh\nnode main.mjs caller.wav interruption.wav\n```\n\nThe second clip starts while reply audio is queued. `onBargeIn` clears that\nsimulated queue, and capture observes at least five seconds after that clip ends.\nAn interrupted answer can correctly fail the full-answer check;\ninspect the separate interruption evidence. This is not a physical speaker test.\nFor a real playback adapter, cancel both queued and currently playing audio.\nReplace the example lookup with authorized application data before product use.\n\nOn a session error, the program exits with JSON containing a recognized machine\n`code` (for example `media_dead` or `invalid_configure`). Unknown server codes\nbecome `unrecognized_server_error`; transport or decoding errors without a\nrecognized transport code become `transport_or_protocol_error`. Raw error text,\ncall IDs and unknown code values are omitted. No automatic retry is attempted;\ninspect the code and your authorized diagnostics before deciding to retry.\n";
|
|
3
|
+
"package.json": "{\n \"name\": \"pyai-omni-starter\",\n \"version\": \"0.0.0\",\n \"private\": true,\n \"type\": \"module\",\n \"engines\": {\n \"node\": \">=20.19.0\"\n },\n \"scripts\": {\n \"start\": \"node main.mjs\"\n },\n \"dependencies\": {\n \"@pyai/sdk\": \"0.13.0\",\n \"ws\": \"^8.18.3\"\n }\n}\n";
|
|
4
4
|
"timing.mjs": "// Monotonic capture timing. Arrival time is distinct from queued playback:\n// a single packet may contain seconds of audio and must finish before settling.\nexport const FRAME_MS = 20;\nexport const REALTIME_GAP_LIMIT_MS = 100;\n// Interruption tests need an observation window after caller transmission,\n// even when an earlier cue/response already finished. These are test bounds,\n// not an inferred engine reply-end or a naturalness threshold.\nexport const INTERRUPTION_OBSERVATION = Object.freeze({\n minimumAfterCallerStreamMs: 5000, quietMs: 2000, maximumAfterCallerStreamMs: 25000,\n});\n\n/** Approximate audible bounds; this is energy detection, not a speech/VAD claim. */\nexport function audibleBounds(pcm, rate, rmsFloor = 160) {\n const window = Math.max(1, Math.round(rate / 100)); // 10 ms windows\n let first = null;\n let last = null;\n for (let off = 0; off < pcm.length; off += window) {\n const end = Math.min(pcm.length, off + window);\n let energy = 0;\n for (let i = off; i < end; i++) energy += pcm[i] * pcm[i];\n if (Math.sqrt(energy / (end - off)) >= rmsFloor) {\n first ??= off;\n last = end;\n }\n }\n return { first, last };\n}\n\nexport function newAudioCapture(started) {\n return {\n started, firstPacketAt: null, lastPacketAt: null,\n firstAudioAt: null, lastAudioAt: null, playbackEndAt: null,\n samples: 0, pcm: [], turnBeginAt: null, eouMs: null,\n latestTurnBeginAt: null, postTurnBeginFirstPacketAt: null,\n postTurnBeginFirstAudioAt: null, lastAssistantTranscriptAt: null, lastCallerTranscriptAt: null,\n callerTranscriptEvents: [], assistantTranscriptEvents: [],\n turnBegins: [], unheardCancellations: [], unheardSupersededTurns: [], tools: [], toolResults: [], kb: null, events: [],\n };\n}\n\nexport function recordTurnBegin(ctx, at, turn, origin = 0) {\n const prior = ctx.turnBegins.at(-1);\n const cancellation = ctx.unheardCancellations.at(-1);\n if (prior && cancellation && !cancellation.admitted\n && cancellation.turn === prior.turn && cancellation.turnBeginIndex === ctx.turnBegins.length - 1\n && Number.isSafeInteger(turn) && turn > prior.turn && at > cancellation.at\n && ctx.samples === 0 && ctx.assistantTranscriptEvents.length === 0\n && ctx.tools.length === 0 && ctx.toolResults.length === 0) {\n cancellation.admitted = true;\n ctx.unheardSupersededTurns.push({ turn: prior.turn, beginAtMs: prior.atMs,\n cancelledAtMs: cancellation.atMs, cancellationEventIndex: cancellation.eventIndex,\n replacementTurn: turn, outputPacketsBeforeReplacement: 0 });\n }\n ctx.turnBeginAt ??= at;\n ctx.latestTurnBeginAt = at;\n // Earlier PCM can be a listening cue. A new reply is still pending even\n // when that cue has already finished playing; retain all of its PCM.\n ctx.postTurnBeginFirstPacketAt = null;\n ctx.postTurnBeginFirstAudioAt = null;\n ctx.turnBegins.push({ atMs: Math.round(at - origin), turn: turn ?? null });\n}\n\nexport function recordUnheardCancellation(ctx, event, at, origin, eventIndex) {\n const prior = ctx.turnBegins.at(-1);\n if (event.event !== \"flush\" || event.reason !== \"turn_merge\"\n || !Number.isSafeInteger(event.cancelled_turn) || event.cancelled_turn <= 0\n || prior?.turn !== event.cancelled_turn || at < ctx.latestTurnBeginAt\n || ctx.samples !== 0 || ctx.assistantTranscriptEvents.length\n || ctx.tools.length || ctx.toolResults.length\n || ctx.unheardCancellations.some(c => c.turnBeginIndex === ctx.turnBegins.length - 1)) return;\n ctx.unheardCancellations.push({ turn: event.cancelled_turn, at,\n atMs: Math.round(at - origin), eventIndex, turnBeginIndex: ctx.turnBegins.length - 1,\n admitted: false });\n}\n\nexport function recordAudio(ctx, pcm, at, rate) {\n if (!(pcm instanceof Int16Array) || !pcm.length) return null;\n const copy = pcm.slice(); // Some transports reuse callback buffers.\n const playbackAt = Math.max(at, ctx.playbackEndAt ?? at);\n const endAt = playbackAt + (copy.length / rate) * 1000;\n const bounds = audibleBounds(copy, rate);\n ctx.firstPacketAt ??= at;\n ctx.lastPacketAt = at;\n ctx.playbackEndAt = endAt;\n const afterTurnBegin = ctx.latestTurnBeginAt != null && at >= ctx.latestTurnBeginAt;\n if (afterTurnBegin) ctx.postTurnBeginFirstPacketAt ??= at;\n if (bounds.first != null) {\n ctx.firstAudioAt ??= playbackAt + (bounds.first / rate) * 1000;\n ctx.lastAudioAt = playbackAt + (bounds.last / rate) * 1000;\n if (afterTurnBegin) ctx.postTurnBeginFirstAudioAt ??= playbackAt + (bounds.first / rate) * 1000;\n }\n ctx.samples += copy.length;\n ctx.pcm.push(copy);\n return { playbackAt, endAt, pcm: copy };\n}\n\n/** Preserve negative gaps: they are overlaps, never missing measurements. */\nexport function captureTiming(ctx, caller, origin = 0) {\n const relative = (at) => at == null ? null : Math.round(at - origin);\n const gap = (at) => at == null || caller.speechOffsetAt == null\n ? null : Math.round(at - caller.speechOffsetAt);\n return {\n ttfbMs: gap(ctx.firstAudioAt),\n anyAudioTtfbMs: gap(ctx.firstAudioAt),\n postTurnBeginTtfbMs: gap(ctx.postTurnBeginFirstAudioAt),\n turnMs: gap(ctx.lastAudioAt),\n packetTtfbMs: gap(ctx.firstPacketAt),\n callerStartMs: relative(caller.startedAt),\n callerSpeechOnsetMs: relative(caller.speechOnsetAt),\n callerSpeechOffsetMs: relative(caller.speechOffsetAt),\n callerStreamEndMs: relative(caller.streamEndAt),\n agentFirstPacketMs: relative(ctx.firstPacketAt),\n agentLastPacketMs: relative(ctx.lastPacketAt),\n agentSpeechOnsetMs: relative(ctx.firstAudioAt),\n agentSpeechOffsetMs: relative(ctx.lastAudioAt),\n agentPlaybackEndMs: relative(ctx.playbackEndAt),\n latestTurnBeginMs: relative(ctx.latestTurnBeginAt),\n postTurnBeginFirstPacketMs: relative(ctx.postTurnBeginFirstPacketAt),\n postTurnBeginSpeechOnsetMs: relative(ctx.postTurnBeginFirstAudioAt),\n lastAssistantTranscriptMs: relative(ctx.lastAssistantTranscriptAt),\n callerMaxFrameGapMs: Math.round(caller.maxFrameGapMs ?? 0),\n method: \"client-monotonic-queued-playback-energy-bounds\",\n };\n}\n\nexport async function streamPcmRealtime(omni, pcm, rate, clock, onFrame = () => {}) {\n const frameSize = Math.max(1, Math.round((rate * FRAME_MS) / 1000));\n const bounds = audibleBounds(pcm, rate);\n const startedAt = clock.now();\n let speechOnsetAt = null;\n let speechOffsetAt = null;\n let previousFrameEnd = startedAt;\n let maxFrameGapMs = 0;\n for (let off = 0; off < pcm.length; off += frameSize) {\n const frame = pcm.subarray(off, Math.min(off + frameSize, pcm.length));\n const at = clock.now();\n maxFrameGapMs = Math.max(maxFrameGapMs, at - previousFrameEnd);\n const durationMs = (frame.length / rate) * 1000;\n omni.sendAudio(frame);\n onFrame(frame, at);\n if (bounds.first != null && bounds.first >= off && bounds.first < off + frame.length) {\n speechOnsetAt = at + ((bounds.first - off) / rate) * 1000;\n }\n if (bounds.last != null && bounds.last > off && bounds.last <= off + frame.length) {\n speechOffsetAt = at + ((bounds.last - off) / rate) * 1000;\n }\n previousFrameEnd = at + durationMs;\n // Pace from the actual send. Never burst late frames to \"catch up\".\n await clock.sleep(Math.max(0, previousFrameEnd - clock.now()));\n }\n return { startedAt, speechOnsetAt, speechOffsetAt,\n streamEndAt: clock.now(), maxFrameGapMs };\n}\n\nexport async function streamSilenceWhile(omni, rate, shouldContinue, clock, onFrame = () => {}) {\n const silence = new Int16Array(Math.max(1, Math.round((rate * FRAME_MS) / 1000)));\n while (shouldContinue()) {\n const at = clock.now();\n omni.sendAudio(silence);\n onFrame(silence, at);\n await clock.sleep(FRAME_MS);\n }\n}\n\nexport async function waitForAgentSettle(getCtx, clock, opts = {}) {\n const started = clock.now();\n const observation = opts.callerStreamEndAt != null;\n if (observation && (!Number.isFinite(opts.callerStreamEndAt)\n || opts.callerStreamEndAt < 0 || opts.callerStreamEndAt > started)) {\n throw new Error(\"Invalid caller observation boundary\");\n }\n const timeoutMs = observation ? INTERRUPTION_OBSERVATION.maximumAfterCallerStreamMs : opts.timeoutMs ?? 25000;\n const settleMs = observation ? INTERRUPTION_OBSERVATION.quietMs : opts.settleMs ?? 2000;\n const deadlineAt = (observation ? opts.callerStreamEndAt : started) + timeoutMs;\n const minimumAt = observation ? opts.callerStreamEndAt + INTERRUPTION_OBSERVATION.minimumAfterCallerStreamMs : -Infinity;\n while (true) {\n const ctx = getCtx();\n const at = clock.now();\n if (opts.isClosed?.()) return { reason: \"closed\", at };\n if (observation && at >= deadlineAt) return { reason: \"timeout\", at };\n const hasResponseAudio = !opts.requireTurnBegin || (ctx.latestTurnBeginAt != null\n && ctx.postTurnBeginFirstAudioAt != null);\n // Synthesis text is progress, not a completion event. It can arrive after\n // early/final audio, so extend the quiet floor without demanding another\n // packet after every advisory. The protocol still has no reply-end marker.\n const activityEnd = Math.max(ctx.playbackEndAt ?? -Infinity,\n ctx.latestTurnBeginAt ?? -Infinity, ctx.lastAssistantTranscriptAt ?? -Infinity,\n observation ? ctx.lastCallerTranscriptAt ?? -Infinity : -Infinity);\n // Do not demand a new post-caller turn_begin: an earlier turn can be\n // coalesced and speak later. This records a bounded quiet observation;\n // downstream response-timing checks can impose a stricter begin boundary.\n if (ctx.firstPacketAt != null && hasResponseAudio && at >= minimumAt && at >= activityEnd + settleMs) {\n return { reason: \"settled\", at };\n }\n if (opts.allowEmpty && ctx.firstPacketAt == null && at >= minimumAt && at - started >= (opts.emptyWaitMs ?? 800)) {\n return { reason: \"empty\", at };\n }\n if (at - started >= timeoutMs) return { reason: \"timeout\", at };\n await clock.sleep(observation ? Math.min(20, deadlineAt - at) : 20);\n }\n}\n\nexport async function withTimeout(promise, ms, message, clock) {\n let timer;\n try {\n return await Promise.race([\n promise,\n new Promise((_, reject) => {\n timer = clock.setTimeout(() => reject(new Error(message)), ms);\n }),\n ]);\n } finally {\n clock.clearTimeout(timer);\n }\n}\n";
|
|
5
5
|
"main.mjs": "import PyAI from \"@pyai/sdk\";\nimport WebSocket from \"ws\"; // Explicit transport makes this work on Node 20 too.\nimport { readFile, writeFile, mkdtemp } from \"node:fs/promises\";\nimport { join } from \"node:path\";\nimport { runSession } from \"./session.mjs\";\nimport { readWav, writeWav, assessAnswer } from \"./audio.mjs\";\n\nasync function main() {\n const [callerPath, interruptionPath, extra] = process.argv.slice(2);\n if (!callerPath || extra) throw new Error(\"Usage: node main.mjs caller.wav [interruption.wav]\");\n if (!process.env.PYAI_API_KEY) throw new Error(\"Inject PYAI_API_KEY through your environment first\");\n const caller = readWav(await readFile(callerPath));\n const interruption = interruptionPath ? readWav(await readFile(interruptionPath)) : undefined;\n const pyai = new PyAI({ apiKey: process.env.PYAI_API_KEY, maxRetries: 0,\n fetch: (url, options) => fetch(url, { ...options, signal: AbortSignal.timeout(30000) }),\n });\n const { report, audio, replyAudio } = await runSession({ pyai, webSocket: WebSocket, caller, interruption });\n const directory = await mkdtemp(join(process.cwd(), \"omni-run-\"));\n const save = (name, data) => writeFile(join(directory, name), data, { mode: 0o600, flag: \"wx\" });\n await save(\"session.wav\", writeWav(audio, report.output_rate ?? 24000));\n const reply = writeWav(replyAudio, report.output_rate ?? 24000);\n await save(\"reply.wav\", reply);\n // Hear the captured bytes, rather than trusting synthesis-advisory text.\n let capturedTranscript = \"\";\n if (report.reply_audio_bytes) {\n try {\n const heard = await pyai.audio.transcriptions.create({ file: new File([reply], \"reply.wav\", { type: \"audio/wav\" }), model: \"pyai-hear\" });\n capturedTranscript = heard.text;\n } catch { report.hear_error = \"Captured audio saved; Hear verification failed\"; }\n }\n const result = { ...report, captured_transcript: capturedTranscript, evidence: assessAnswer(report, capturedTranscript) };\n await save(\"report.json\", JSON.stringify(result, null, 2) + \"\\n\");\n console.log(JSON.stringify({ directory, ...result }, null, 2));\n if (result.evidence.captured_answer !== \"verified_by_hear\" || (interruption && result.evidence.interruption !== \"simulated_queue_cleared\")) process.exitCode = 1;\n}\n\nmain().catch(error => {\n const message = String(error.message).replaceAll(process.env.PYAI_API_KEY || \"\\0\", \"[REDACTED]\");\n console.error(JSON.stringify({ error: message, ...(error.omni_code ? { code: error.omni_code } : {}) })); process.exitCode = 1;\n});\n";
|
|
6
6
|
"session.mjs": "import { newAudioCapture, recordAudio, recordTurnBegin, streamPcmRealtime, INTERRUPTION_OBSERVATION } from \"./timing.mjs\";\n\nexport const realClock = {\n now: () => performance.now(),\n sleep: ms => new Promise(resolve => setTimeout(resolve, ms)),\n};\n\n// Only recognized machine codes may leave the example. Error messages, call\n// IDs and arbitrary code values can contain credentials or customer content.\nconst publicErrorCodes = new Set([\n \"invalid_configure\", \"unsupported_tool_transport\", \"tool_configuration_locked\",\n \"media_dead\", \"unauthorized\", \"insufficient_scope\", \"rate_limit_exceeded\",\n \"concurrency_limit_exceeded\", \"daily_cap_exceeded\", \"credit_exhausted\",\n]);\nconst transportErrorCodes = new Set([\"ECONNRESET\", \"ECONNREFUSED\", \"ETIMEDOUT\", \"ENOTFOUND\", \"EAI_AGAIN\"]);\nexport function omniFailure(error) {\n const code = error instanceof Error\n ? (transportErrorCodes.has(error.code) ? error.code : \"transport_or_protocol_error\")\n : (publicErrorCodes.has(error?.code) ? error.code : \"unrecognized_server_error\");\n return Object.assign(new Error(`Omni failed (${code}); no automatic retry was attempted`), { omni_code: code });\n}\n\n/** One bounded, server-side example. Its playback queue is simulated. */\nexport async function runSession({ pyai, webSocket, caller, interruption, clock = realClock, timeoutMs = 60000 }) {\n const rate = 24000, frameSize = 480, silence = new Int16Array(frameSize);\n if (!(caller instanceof Int16Array) || !caller.length || caller.length > rate * 20) throw new Error(\"Provide 1–20 seconds of caller PCM16 at 24 kHz\");\n if (interruption && (!(interruption instanceof Int16Array) || !interruption.length || interruption.length > rate * 20)) throw new Error(\"Invalid interruption PCM\");\n const started = clock.now(), all = newAudioCapture(started), reply = newAudioCapture(started);\n const report = { input_rate: rate, output_rate: null, playback_sink: \"simulated\", tool_calls: 0, tool_executions: 0, duplicate_tool_calls: 0, flushes_after_interruption: 0, cleared_queue_ms: 0, interruption_requested: !!interruption, caller_started_ms: null, interruption_started_ms: null, advisory_transcripts: [], end_reason: \"timeout\" };\n let session, configuredAt = null, closed = false, failure = null, phase = \"greeting\", offset = 0, queueEnd = started, interruptionSent = false, inputEndAt = started;\n const handled = new Set();\n const fail = message => { failure ??= new Error(message); };\n try {\n session = pyai.omni.connect({\n webSocket, rate,\n configure: {\n voice_id: \"stock_amos_en_us\", language: \"en\", greeting: \"Hello. How can I help?\",\n persona: \"When asked about office hours, call lookup_office_hours. Never guess. After the result, say one short sentence giving its opening_time.\",\n tools: [{ name: \"lookup_office_hours\", description: \"Read the synthetic example office opening time.\", side_effect: \"read\", parameters: { type: \"object\", properties: {}, required: [], additionalProperties: false } }],\n },\n onHello: frame => {\n const match = /^pcm16@(8000|24000)$/.exec(frame.audio_out ?? \"\");\n if (!match) return fail(\"Unsupported Omni output format\");\n report.output_rate = Number(match[1]);\n },\n onConfigured: () => { configuredAt ??= clock.now(); },\n onAudio: chunk => {\n if (!report.output_rate) return fail(\"Audio arrived before its output format\");\n const bytes = chunk instanceof ArrayBuffer ? new Uint8Array(chunk) : new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength);\n if (bytes.byteLength % 2) return fail(\"Invalid PCM16 output\");\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n const pcm = Int16Array.from({ length: bytes.length / 2 }, (_, i) => view.getInt16(i * 2, true));\n const now = clock.now();\n recordAudio(all, pcm, now, report.output_rate);\n queueEnd = Math.max(now, queueEnd) + pcm.length / report.output_rate * 1000;\n if (report.tool_executions) recordAudio(reply, pcm, now, report.output_rate);\n },\n onEvent: frame => {\n if (frame.event === \"turn_begin\") recordTurnBegin(all, clock.now(), frame.turn);\n },\n onTranscript: frame => {\n // An assistant advisory is progress; never treat it as received speech.\n if (report.advisory_transcripts.length < 100) report.advisory_transcripts.push({ role: frame.role, text: frame.text });\n if (frame.role === \"assistant\") all.lastAssistantTranscriptAt = clock.now();\n },\n onToolCall: frame => {\n report.tool_calls++;\n if (handled.has(frame.call_id)) { report.duplicate_tool_calls++; return; }\n handled.add(frame.call_id);\n if (frame.name !== \"lookup_office_hours\") { session.toolResult(frame.call_id, { error: \"Unknown example tool\" }); return; }\n // Replace this read-only fixture with your authorized data lookup.\n report.tool_executions++;\n session.toolResult(frame.call_id, { result: { opening_time: \"9 a.m.\", source: \"synthetic example office\" } });\n },\n onBargeIn: () => {\n const now = clock.now(), queued = Math.max(0, queueEnd - now);\n if (report.interruption_started_ms != null) { report.flushes_after_interruption++; report.cleared_queue_ms += queued; }\n // A real speaker adapter must cancel scheduled and currently playing audio.\n queueEnd = now; all.playbackEndAt = now; reply.playbackEndAt = now;\n },\n onError: error => { failure ??= omniFailure(error); },\n onClose: () => { closed = true; },\n });\n while (clock.now() - started < timeoutMs) {\n if (failure) throw failure;\n if (closed) { report.end_reason = \"closed\"; break; }\n const now = clock.now();\n if (configuredAt == null || !report.output_rate) {\n if (now - started >= 10000) throw new Error(\"Omni connection/configuration timed out\");\n await clock.sleep(20); continue;\n }\n const quietAt = Math.max(queueEnd, all.lastPacketAt ?? now, all.lastAssistantTranscriptAt ?? 0, inputEndAt);\n if (phase === \"greeting\") {\n if (all.firstPacketAt != null && now - configuredAt >= 2000 && now >= quietAt + 800) {\n phase = \"caller\"; report.caller_started_ms = now - started;\n } else if (now - configuredAt > 15000) throw new Error(\"Greeting did not drain within 15 seconds\");\n }\n if (phase === \"reply\" && interruption && !interruptionSent && reply.samples && queueEnd - now >= 200) {\n phase = \"interruption\"; offset = 0; interruptionSent = true; report.interruption_started_ms = now - started;\n }\n const observationEnd = inputEndAt + (interruptionSent ? INTERRUPTION_OBSERVATION.minimumAfterCallerStreamMs : 0);\n if (phase === \"reply\" && reply.samples && now >= quietAt + 2000 && now >= observationEnd && (!interruption || interruptionSent)) {\n report.end_reason = \"quiet_window\"; break;\n }\n // Exactly one producer: each 20 ms slot contains caller PCM OR silence.\n let pcm = silence;\n if (phase === \"caller\" || phase === \"interruption\") {\n const source = phase === \"caller\" ? caller : interruption;\n pcm = source.subarray(offset, offset + frameSize); offset += pcm.length;\n if (offset >= source.length) {\n inputEndAt = now + pcm.length / rate * 1000;\n report[phase === \"caller\" ? \"caller_completed_ms\" : \"interruption_completed_ms\"] = inputEndAt - started;\n phase = \"reply\"; offset = 0;\n }\n }\n await streamPcmRealtime(session, pcm, rate, clock);\n }\n report.audio_bytes = all.samples * 2;\n report.reply_audio_bytes = reply.samples * 2;\n report.elapsed_ms = Math.round(clock.now() - started);\n return { report, audio: all.pcm, replyAudio: reply.pcm };\n } finally { session?.close(); }\n}\n";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Generated by scripts/generate-omni-starter.mjs; edit examples/omni-starter instead.
|
|
2
2
|
export const omniTemplate = Object.freeze({
|
|
3
|
-
"README.md": "# Runnable Omni starter\n\nRequires Node 20.19+ and ESM. Node 20 uses the included `ws` transport;\nNode 22 also works. The separate MCP server requires Node 22+.\nThis server-side example uses a synthetic read-only office-hours tool. Read\n`PYAI.md` when generating the project through the CLI.\n\n<!-- omni-install:start -->\n```sh\nnpm install @pyai/sdk@0.
|
|
4
|
-
"package.json": "{\n \"name\": \"pyai-omni-starter\",\n \"version\": \"0.0.0\",\n \"private\": true,\n \"type\": \"module\",\n \"engines\": {\n \"node\": \">=20.19.0\"\n },\n \"scripts\": {\n \"start\": \"node main.mjs\"\n },\n \"dependencies\": {\n \"@pyai/sdk\": \"0.
|
|
3
|
+
"README.md": "# Runnable Omni starter\n\nRequires Node 20.19+ and ESM. Node 20 uses the included `ws` transport;\nNode 22 also works. The separate MCP server requires Node 22+.\nThis server-side example uses a synthetic read-only office-hours tool. Read\n`PYAI.md` when generating the project through the CLI.\n\n<!-- omni-install:start -->\n```sh\nnpm install @pyai/sdk@0.13.0\nnpx pyai init voice-demo --template omni\ncd voice-demo\nnpm install\n```\n\nInitialization is offline; installing dependencies and running `main.mjs` are\nseparate steps. For an existing checkout of this example, run `npm install`\nin its directory instead.\n<!-- omni-install:end -->\n\nInject `PYAI_API_KEY` through your secret manager or shell environment. Do not\npaste a real key into source, a prompt, or shell history. `.env.example` documents\nthe variable; this program does not load `.env` or saved CLI profiles.\n\nPrepare a 24 kHz PCM16 mono WAV, at most 20 seconds, saying “Please look up the\noffice opening time.” Then run:\n\n```sh\nnode main.mjs caller.wav\n```\n\nRunning the program explicitly opens one bounded Omni session and uploads its\ncaptured reply to Hear. Both consume your key's allowed usage. Automatic retries\nare disabled. No managed number is bought and no phone call is placed.\n\n`session.mjs` waits for configuration and the greeting to drain, sends exactly\none paced input stream (caller PCM or silence), returns the read tool's result,\nand captures output using the rate from `hello.audio_out`. Never run a separate\nsilence timer alongside caller audio. `timing.mjs` is generated from the existing\nOmni evaluation harness's timing utilities.\n\nThe new private `omni-run-*` folder contains `session.wav`, `reply.wav`, and\n`report.json`. The report separates received audio, an office-hours answer\nrecovered by Hear from captured audio, and physical playback (not tested).\nThe office-hours check accepts formatting such as `opens@9a.m`, `9 a.m.` and\n`nine in the morning` in a complete affirmative opening-time sentence. Wrong\ntimes, negation, conflicting or truncated text remain unverified. This is a\nconservative check for the example fact, not a general answer-quality score.\nA synthesis transcript is advisory; it is not evidence that speech arrived.\nThis starter does not negotiate playback receipts. Capture ends after the\nsimulated queue drains and two seconds of quiet, a bounded capture heuristic.\nListen to the saved file when qualifying actual sound quality or completeness.\n\nTo exercise interruption, supply a second WAV saying “Stop speaking now”:\n\n```sh\nnode main.mjs caller.wav interruption.wav\n```\n\nThe second clip starts while reply audio is queued. `onBargeIn` clears that\nsimulated queue, and capture observes at least five seconds after that clip ends.\nAn interrupted answer can correctly fail the full-answer check;\ninspect the separate interruption evidence. This is not a physical speaker test.\nFor a real playback adapter, cancel both queued and currently playing audio.\nReplace the example lookup with authorized application data before product use.\n\nOn a session error, the program exits with JSON containing a recognized machine\n`code` (for example `media_dead` or `invalid_configure`). Unknown server codes\nbecome `unrecognized_server_error`; transport or decoding errors without a\nrecognized transport code become `transport_or_protocol_error`. Raw error text,\ncall IDs and unknown code values are omitted. No automatic retry is attempted;\ninspect the code and your authorized diagnostics before deciding to retry.\n",
|
|
4
|
+
"package.json": "{\n \"name\": \"pyai-omni-starter\",\n \"version\": \"0.0.0\",\n \"private\": true,\n \"type\": \"module\",\n \"engines\": {\n \"node\": \">=20.19.0\"\n },\n \"scripts\": {\n \"start\": \"node main.mjs\"\n },\n \"dependencies\": {\n \"@pyai/sdk\": \"0.13.0\",\n \"ws\": \"^8.18.3\"\n }\n}\n",
|
|
5
5
|
"timing.mjs": "// Monotonic capture timing. Arrival time is distinct from queued playback:\n// a single packet may contain seconds of audio and must finish before settling.\nexport const FRAME_MS = 20;\nexport const REALTIME_GAP_LIMIT_MS = 100;\n// Interruption tests need an observation window after caller transmission,\n// even when an earlier cue/response already finished. These are test bounds,\n// not an inferred engine reply-end or a naturalness threshold.\nexport const INTERRUPTION_OBSERVATION = Object.freeze({\n minimumAfterCallerStreamMs: 5000, quietMs: 2000, maximumAfterCallerStreamMs: 25000,\n});\n\n/** Approximate audible bounds; this is energy detection, not a speech/VAD claim. */\nexport function audibleBounds(pcm, rate, rmsFloor = 160) {\n const window = Math.max(1, Math.round(rate / 100)); // 10 ms windows\n let first = null;\n let last = null;\n for (let off = 0; off < pcm.length; off += window) {\n const end = Math.min(pcm.length, off + window);\n let energy = 0;\n for (let i = off; i < end; i++) energy += pcm[i] * pcm[i];\n if (Math.sqrt(energy / (end - off)) >= rmsFloor) {\n first ??= off;\n last = end;\n }\n }\n return { first, last };\n}\n\nexport function newAudioCapture(started) {\n return {\n started, firstPacketAt: null, lastPacketAt: null,\n firstAudioAt: null, lastAudioAt: null, playbackEndAt: null,\n samples: 0, pcm: [], turnBeginAt: null, eouMs: null,\n latestTurnBeginAt: null, postTurnBeginFirstPacketAt: null,\n postTurnBeginFirstAudioAt: null, lastAssistantTranscriptAt: null, lastCallerTranscriptAt: null,\n callerTranscriptEvents: [], assistantTranscriptEvents: [],\n turnBegins: [], unheardCancellations: [], unheardSupersededTurns: [], tools: [], toolResults: [], kb: null, events: [],\n };\n}\n\nexport function recordTurnBegin(ctx, at, turn, origin = 0) {\n const prior = ctx.turnBegins.at(-1);\n const cancellation = ctx.unheardCancellations.at(-1);\n if (prior && cancellation && !cancellation.admitted\n && cancellation.turn === prior.turn && cancellation.turnBeginIndex === ctx.turnBegins.length - 1\n && Number.isSafeInteger(turn) && turn > prior.turn && at > cancellation.at\n && ctx.samples === 0 && ctx.assistantTranscriptEvents.length === 0\n && ctx.tools.length === 0 && ctx.toolResults.length === 0) {\n cancellation.admitted = true;\n ctx.unheardSupersededTurns.push({ turn: prior.turn, beginAtMs: prior.atMs,\n cancelledAtMs: cancellation.atMs, cancellationEventIndex: cancellation.eventIndex,\n replacementTurn: turn, outputPacketsBeforeReplacement: 0 });\n }\n ctx.turnBeginAt ??= at;\n ctx.latestTurnBeginAt = at;\n // Earlier PCM can be a listening cue. A new reply is still pending even\n // when that cue has already finished playing; retain all of its PCM.\n ctx.postTurnBeginFirstPacketAt = null;\n ctx.postTurnBeginFirstAudioAt = null;\n ctx.turnBegins.push({ atMs: Math.round(at - origin), turn: turn ?? null });\n}\n\nexport function recordUnheardCancellation(ctx, event, at, origin, eventIndex) {\n const prior = ctx.turnBegins.at(-1);\n if (event.event !== \"flush\" || event.reason !== \"turn_merge\"\n || !Number.isSafeInteger(event.cancelled_turn) || event.cancelled_turn <= 0\n || prior?.turn !== event.cancelled_turn || at < ctx.latestTurnBeginAt\n || ctx.samples !== 0 || ctx.assistantTranscriptEvents.length\n || ctx.tools.length || ctx.toolResults.length\n || ctx.unheardCancellations.some(c => c.turnBeginIndex === ctx.turnBegins.length - 1)) return;\n ctx.unheardCancellations.push({ turn: event.cancelled_turn, at,\n atMs: Math.round(at - origin), eventIndex, turnBeginIndex: ctx.turnBegins.length - 1,\n admitted: false });\n}\n\nexport function recordAudio(ctx, pcm, at, rate) {\n if (!(pcm instanceof Int16Array) || !pcm.length) return null;\n const copy = pcm.slice(); // Some transports reuse callback buffers.\n const playbackAt = Math.max(at, ctx.playbackEndAt ?? at);\n const endAt = playbackAt + (copy.length / rate) * 1000;\n const bounds = audibleBounds(copy, rate);\n ctx.firstPacketAt ??= at;\n ctx.lastPacketAt = at;\n ctx.playbackEndAt = endAt;\n const afterTurnBegin = ctx.latestTurnBeginAt != null && at >= ctx.latestTurnBeginAt;\n if (afterTurnBegin) ctx.postTurnBeginFirstPacketAt ??= at;\n if (bounds.first != null) {\n ctx.firstAudioAt ??= playbackAt + (bounds.first / rate) * 1000;\n ctx.lastAudioAt = playbackAt + (bounds.last / rate) * 1000;\n if (afterTurnBegin) ctx.postTurnBeginFirstAudioAt ??= playbackAt + (bounds.first / rate) * 1000;\n }\n ctx.samples += copy.length;\n ctx.pcm.push(copy);\n return { playbackAt, endAt, pcm: copy };\n}\n\n/** Preserve negative gaps: they are overlaps, never missing measurements. */\nexport function captureTiming(ctx, caller, origin = 0) {\n const relative = (at) => at == null ? null : Math.round(at - origin);\n const gap = (at) => at == null || caller.speechOffsetAt == null\n ? null : Math.round(at - caller.speechOffsetAt);\n return {\n ttfbMs: gap(ctx.firstAudioAt),\n anyAudioTtfbMs: gap(ctx.firstAudioAt),\n postTurnBeginTtfbMs: gap(ctx.postTurnBeginFirstAudioAt),\n turnMs: gap(ctx.lastAudioAt),\n packetTtfbMs: gap(ctx.firstPacketAt),\n callerStartMs: relative(caller.startedAt),\n callerSpeechOnsetMs: relative(caller.speechOnsetAt),\n callerSpeechOffsetMs: relative(caller.speechOffsetAt),\n callerStreamEndMs: relative(caller.streamEndAt),\n agentFirstPacketMs: relative(ctx.firstPacketAt),\n agentLastPacketMs: relative(ctx.lastPacketAt),\n agentSpeechOnsetMs: relative(ctx.firstAudioAt),\n agentSpeechOffsetMs: relative(ctx.lastAudioAt),\n agentPlaybackEndMs: relative(ctx.playbackEndAt),\n latestTurnBeginMs: relative(ctx.latestTurnBeginAt),\n postTurnBeginFirstPacketMs: relative(ctx.postTurnBeginFirstPacketAt),\n postTurnBeginSpeechOnsetMs: relative(ctx.postTurnBeginFirstAudioAt),\n lastAssistantTranscriptMs: relative(ctx.lastAssistantTranscriptAt),\n callerMaxFrameGapMs: Math.round(caller.maxFrameGapMs ?? 0),\n method: \"client-monotonic-queued-playback-energy-bounds\",\n };\n}\n\nexport async function streamPcmRealtime(omni, pcm, rate, clock, onFrame = () => {}) {\n const frameSize = Math.max(1, Math.round((rate * FRAME_MS) / 1000));\n const bounds = audibleBounds(pcm, rate);\n const startedAt = clock.now();\n let speechOnsetAt = null;\n let speechOffsetAt = null;\n let previousFrameEnd = startedAt;\n let maxFrameGapMs = 0;\n for (let off = 0; off < pcm.length; off += frameSize) {\n const frame = pcm.subarray(off, Math.min(off + frameSize, pcm.length));\n const at = clock.now();\n maxFrameGapMs = Math.max(maxFrameGapMs, at - previousFrameEnd);\n const durationMs = (frame.length / rate) * 1000;\n omni.sendAudio(frame);\n onFrame(frame, at);\n if (bounds.first != null && bounds.first >= off && bounds.first < off + frame.length) {\n speechOnsetAt = at + ((bounds.first - off) / rate) * 1000;\n }\n if (bounds.last != null && bounds.last > off && bounds.last <= off + frame.length) {\n speechOffsetAt = at + ((bounds.last - off) / rate) * 1000;\n }\n previousFrameEnd = at + durationMs;\n // Pace from the actual send. Never burst late frames to \"catch up\".\n await clock.sleep(Math.max(0, previousFrameEnd - clock.now()));\n }\n return { startedAt, speechOnsetAt, speechOffsetAt,\n streamEndAt: clock.now(), maxFrameGapMs };\n}\n\nexport async function streamSilenceWhile(omni, rate, shouldContinue, clock, onFrame = () => {}) {\n const silence = new Int16Array(Math.max(1, Math.round((rate * FRAME_MS) / 1000)));\n while (shouldContinue()) {\n const at = clock.now();\n omni.sendAudio(silence);\n onFrame(silence, at);\n await clock.sleep(FRAME_MS);\n }\n}\n\nexport async function waitForAgentSettle(getCtx, clock, opts = {}) {\n const started = clock.now();\n const observation = opts.callerStreamEndAt != null;\n if (observation && (!Number.isFinite(opts.callerStreamEndAt)\n || opts.callerStreamEndAt < 0 || opts.callerStreamEndAt > started)) {\n throw new Error(\"Invalid caller observation boundary\");\n }\n const timeoutMs = observation ? INTERRUPTION_OBSERVATION.maximumAfterCallerStreamMs : opts.timeoutMs ?? 25000;\n const settleMs = observation ? INTERRUPTION_OBSERVATION.quietMs : opts.settleMs ?? 2000;\n const deadlineAt = (observation ? opts.callerStreamEndAt : started) + timeoutMs;\n const minimumAt = observation ? opts.callerStreamEndAt + INTERRUPTION_OBSERVATION.minimumAfterCallerStreamMs : -Infinity;\n while (true) {\n const ctx = getCtx();\n const at = clock.now();\n if (opts.isClosed?.()) return { reason: \"closed\", at };\n if (observation && at >= deadlineAt) return { reason: \"timeout\", at };\n const hasResponseAudio = !opts.requireTurnBegin || (ctx.latestTurnBeginAt != null\n && ctx.postTurnBeginFirstAudioAt != null);\n // Synthesis text is progress, not a completion event. It can arrive after\n // early/final audio, so extend the quiet floor without demanding another\n // packet after every advisory. The protocol still has no reply-end marker.\n const activityEnd = Math.max(ctx.playbackEndAt ?? -Infinity,\n ctx.latestTurnBeginAt ?? -Infinity, ctx.lastAssistantTranscriptAt ?? -Infinity,\n observation ? ctx.lastCallerTranscriptAt ?? -Infinity : -Infinity);\n // Do not demand a new post-caller turn_begin: an earlier turn can be\n // coalesced and speak later. This records a bounded quiet observation;\n // downstream response-timing checks can impose a stricter begin boundary.\n if (ctx.firstPacketAt != null && hasResponseAudio && at >= minimumAt && at >= activityEnd + settleMs) {\n return { reason: \"settled\", at };\n }\n if (opts.allowEmpty && ctx.firstPacketAt == null && at >= minimumAt && at - started >= (opts.emptyWaitMs ?? 800)) {\n return { reason: \"empty\", at };\n }\n if (at - started >= timeoutMs) return { reason: \"timeout\", at };\n await clock.sleep(observation ? Math.min(20, deadlineAt - at) : 20);\n }\n}\n\nexport async function withTimeout(promise, ms, message, clock) {\n let timer;\n try {\n return await Promise.race([\n promise,\n new Promise((_, reject) => {\n timer = clock.setTimeout(() => reject(new Error(message)), ms);\n }),\n ]);\n } finally {\n clock.clearTimeout(timer);\n }\n}\n",
|
|
6
6
|
"main.mjs": "import PyAI from \"@pyai/sdk\";\nimport WebSocket from \"ws\"; // Explicit transport makes this work on Node 20 too.\nimport { readFile, writeFile, mkdtemp } from \"node:fs/promises\";\nimport { join } from \"node:path\";\nimport { runSession } from \"./session.mjs\";\nimport { readWav, writeWav, assessAnswer } from \"./audio.mjs\";\n\nasync function main() {\n const [callerPath, interruptionPath, extra] = process.argv.slice(2);\n if (!callerPath || extra) throw new Error(\"Usage: node main.mjs caller.wav [interruption.wav]\");\n if (!process.env.PYAI_API_KEY) throw new Error(\"Inject PYAI_API_KEY through your environment first\");\n const caller = readWav(await readFile(callerPath));\n const interruption = interruptionPath ? readWav(await readFile(interruptionPath)) : undefined;\n const pyai = new PyAI({ apiKey: process.env.PYAI_API_KEY, maxRetries: 0,\n fetch: (url, options) => fetch(url, { ...options, signal: AbortSignal.timeout(30000) }),\n });\n const { report, audio, replyAudio } = await runSession({ pyai, webSocket: WebSocket, caller, interruption });\n const directory = await mkdtemp(join(process.cwd(), \"omni-run-\"));\n const save = (name, data) => writeFile(join(directory, name), data, { mode: 0o600, flag: \"wx\" });\n await save(\"session.wav\", writeWav(audio, report.output_rate ?? 24000));\n const reply = writeWav(replyAudio, report.output_rate ?? 24000);\n await save(\"reply.wav\", reply);\n // Hear the captured bytes, rather than trusting synthesis-advisory text.\n let capturedTranscript = \"\";\n if (report.reply_audio_bytes) {\n try {\n const heard = await pyai.audio.transcriptions.create({ file: new File([reply], \"reply.wav\", { type: \"audio/wav\" }), model: \"pyai-hear\" });\n capturedTranscript = heard.text;\n } catch { report.hear_error = \"Captured audio saved; Hear verification failed\"; }\n }\n const result = { ...report, captured_transcript: capturedTranscript, evidence: assessAnswer(report, capturedTranscript) };\n await save(\"report.json\", JSON.stringify(result, null, 2) + \"\\n\");\n console.log(JSON.stringify({ directory, ...result }, null, 2));\n if (result.evidence.captured_answer !== \"verified_by_hear\" || (interruption && result.evidence.interruption !== \"simulated_queue_cleared\")) process.exitCode = 1;\n}\n\nmain().catch(error => {\n const message = String(error.message).replaceAll(process.env.PYAI_API_KEY || \"\\0\", \"[REDACTED]\");\n console.error(JSON.stringify({ error: message, ...(error.omni_code ? { code: error.omni_code } : {}) })); process.exitCode = 1;\n});\n",
|
|
7
7
|
"session.mjs": "import { newAudioCapture, recordAudio, recordTurnBegin, streamPcmRealtime, INTERRUPTION_OBSERVATION } from \"./timing.mjs\";\n\nexport const realClock = {\n now: () => performance.now(),\n sleep: ms => new Promise(resolve => setTimeout(resolve, ms)),\n};\n\n// Only recognized machine codes may leave the example. Error messages, call\n// IDs and arbitrary code values can contain credentials or customer content.\nconst publicErrorCodes = new Set([\n \"invalid_configure\", \"unsupported_tool_transport\", \"tool_configuration_locked\",\n \"media_dead\", \"unauthorized\", \"insufficient_scope\", \"rate_limit_exceeded\",\n \"concurrency_limit_exceeded\", \"daily_cap_exceeded\", \"credit_exhausted\",\n]);\nconst transportErrorCodes = new Set([\"ECONNRESET\", \"ECONNREFUSED\", \"ETIMEDOUT\", \"ENOTFOUND\", \"EAI_AGAIN\"]);\nexport function omniFailure(error) {\n const code = error instanceof Error\n ? (transportErrorCodes.has(error.code) ? error.code : \"transport_or_protocol_error\")\n : (publicErrorCodes.has(error?.code) ? error.code : \"unrecognized_server_error\");\n return Object.assign(new Error(`Omni failed (${code}); no automatic retry was attempted`), { omni_code: code });\n}\n\n/** One bounded, server-side example. Its playback queue is simulated. */\nexport async function runSession({ pyai, webSocket, caller, interruption, clock = realClock, timeoutMs = 60000 }) {\n const rate = 24000, frameSize = 480, silence = new Int16Array(frameSize);\n if (!(caller instanceof Int16Array) || !caller.length || caller.length > rate * 20) throw new Error(\"Provide 1–20 seconds of caller PCM16 at 24 kHz\");\n if (interruption && (!(interruption instanceof Int16Array) || !interruption.length || interruption.length > rate * 20)) throw new Error(\"Invalid interruption PCM\");\n const started = clock.now(), all = newAudioCapture(started), reply = newAudioCapture(started);\n const report = { input_rate: rate, output_rate: null, playback_sink: \"simulated\", tool_calls: 0, tool_executions: 0, duplicate_tool_calls: 0, flushes_after_interruption: 0, cleared_queue_ms: 0, interruption_requested: !!interruption, caller_started_ms: null, interruption_started_ms: null, advisory_transcripts: [], end_reason: \"timeout\" };\n let session, configuredAt = null, closed = false, failure = null, phase = \"greeting\", offset = 0, queueEnd = started, interruptionSent = false, inputEndAt = started;\n const handled = new Set();\n const fail = message => { failure ??= new Error(message); };\n try {\n session = pyai.omni.connect({\n webSocket, rate,\n configure: {\n voice_id: \"stock_amos_en_us\", language: \"en\", greeting: \"Hello. How can I help?\",\n persona: \"When asked about office hours, call lookup_office_hours. Never guess. After the result, say one short sentence giving its opening_time.\",\n tools: [{ name: \"lookup_office_hours\", description: \"Read the synthetic example office opening time.\", side_effect: \"read\", parameters: { type: \"object\", properties: {}, required: [], additionalProperties: false } }],\n },\n onHello: frame => {\n const match = /^pcm16@(8000|24000)$/.exec(frame.audio_out ?? \"\");\n if (!match) return fail(\"Unsupported Omni output format\");\n report.output_rate = Number(match[1]);\n },\n onConfigured: () => { configuredAt ??= clock.now(); },\n onAudio: chunk => {\n if (!report.output_rate) return fail(\"Audio arrived before its output format\");\n const bytes = chunk instanceof ArrayBuffer ? new Uint8Array(chunk) : new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength);\n if (bytes.byteLength % 2) return fail(\"Invalid PCM16 output\");\n const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n const pcm = Int16Array.from({ length: bytes.length / 2 }, (_, i) => view.getInt16(i * 2, true));\n const now = clock.now();\n recordAudio(all, pcm, now, report.output_rate);\n queueEnd = Math.max(now, queueEnd) + pcm.length / report.output_rate * 1000;\n if (report.tool_executions) recordAudio(reply, pcm, now, report.output_rate);\n },\n onEvent: frame => {\n if (frame.event === \"turn_begin\") recordTurnBegin(all, clock.now(), frame.turn);\n },\n onTranscript: frame => {\n // An assistant advisory is progress; never treat it as received speech.\n if (report.advisory_transcripts.length < 100) report.advisory_transcripts.push({ role: frame.role, text: frame.text });\n if (frame.role === \"assistant\") all.lastAssistantTranscriptAt = clock.now();\n },\n onToolCall: frame => {\n report.tool_calls++;\n if (handled.has(frame.call_id)) { report.duplicate_tool_calls++; return; }\n handled.add(frame.call_id);\n if (frame.name !== \"lookup_office_hours\") { session.toolResult(frame.call_id, { error: \"Unknown example tool\" }); return; }\n // Replace this read-only fixture with your authorized data lookup.\n report.tool_executions++;\n session.toolResult(frame.call_id, { result: { opening_time: \"9 a.m.\", source: \"synthetic example office\" } });\n },\n onBargeIn: () => {\n const now = clock.now(), queued = Math.max(0, queueEnd - now);\n if (report.interruption_started_ms != null) { report.flushes_after_interruption++; report.cleared_queue_ms += queued; }\n // A real speaker adapter must cancel scheduled and currently playing audio.\n queueEnd = now; all.playbackEndAt = now; reply.playbackEndAt = now;\n },\n onError: error => { failure ??= omniFailure(error); },\n onClose: () => { closed = true; },\n });\n while (clock.now() - started < timeoutMs) {\n if (failure) throw failure;\n if (closed) { report.end_reason = \"closed\"; break; }\n const now = clock.now();\n if (configuredAt == null || !report.output_rate) {\n if (now - started >= 10000) throw new Error(\"Omni connection/configuration timed out\");\n await clock.sleep(20); continue;\n }\n const quietAt = Math.max(queueEnd, all.lastPacketAt ?? now, all.lastAssistantTranscriptAt ?? 0, inputEndAt);\n if (phase === \"greeting\") {\n if (all.firstPacketAt != null && now - configuredAt >= 2000 && now >= quietAt + 800) {\n phase = \"caller\"; report.caller_started_ms = now - started;\n } else if (now - configuredAt > 15000) throw new Error(\"Greeting did not drain within 15 seconds\");\n }\n if (phase === \"reply\" && interruption && !interruptionSent && reply.samples && queueEnd - now >= 200) {\n phase = \"interruption\"; offset = 0; interruptionSent = true; report.interruption_started_ms = now - started;\n }\n const observationEnd = inputEndAt + (interruptionSent ? INTERRUPTION_OBSERVATION.minimumAfterCallerStreamMs : 0);\n if (phase === \"reply\" && reply.samples && now >= quietAt + 2000 && now >= observationEnd && (!interruption || interruptionSent)) {\n report.end_reason = \"quiet_window\"; break;\n }\n // Exactly one producer: each 20 ms slot contains caller PCM OR silence.\n let pcm = silence;\n if (phase === \"caller\" || phase === \"interruption\") {\n const source = phase === \"caller\" ? caller : interruption;\n pcm = source.subarray(offset, offset + frameSize); offset += pcm.length;\n if (offset >= source.length) {\n inputEndAt = now + pcm.length / rate * 1000;\n report[phase === \"caller\" ? \"caller_completed_ms\" : \"interruption_completed_ms\"] = inputEndAt - started;\n phase = \"reply\"; offset = 0;\n }\n }\n await streamPcmRealtime(session, pcm, rate, clock);\n }\n report.audio_bytes = all.samples * 2;\n report.reply_audio_bytes = reply.samples * 2;\n report.elapsed_ms = Math.round(clock.now() - started);\n return { report, audio: all.pcm, replyAudio: reply.pcm };\n } finally { session?.close(); }\n}\n",
|
package/dist/cli.js
CHANGED
|
@@ -42,7 +42,7 @@ const commands = [
|
|
|
42
42
|
{ command: "init", description: "Create a new project with API context and request examples offline", args: ["directory"], flags: { template: "string" }, example: "pyai init voice-demo --template typescript" },
|
|
43
43
|
{ command: "smoke", description: "Check models, voices, and speech synthesis", flags: { "tolerate-upstream": "boolean" } },
|
|
44
44
|
{ command: "doctor", description: "Diagnose key, catalogs, and a Speak to Hear round-trip" },
|
|
45
|
-
{ command: "dialer", description: "Open the private desktop calling and meeting-recording app", flags: { port: "string", "no-browser": "boolean", name: "string", "white-label": "boolean" }, example: "pyai dialer --profile production" },
|
|
45
|
+
{ command: "dialer", description: "Open the private desktop calling and meeting-recording app", flags: { port: "string", "no-browser": "boolean", name: "string", "white-label": "boolean", transfers: "boolean" }, example: "pyai dialer --profile production" },
|
|
46
46
|
...routes.map((r) => ({ command: r.command, description: r.description, args: r.id ? ["id"] : [], flags: {
|
|
47
47
|
...(r.body ? bodyFlags : {}), ...(r.binary ? outputFlags : {}), ...(r.wait ? waitFlags : {}),
|
|
48
48
|
...(r.query ?? {}), ...(r.confirm ? { confirm: "boolean" } : {}),
|
|
@@ -512,7 +512,7 @@ async function main() {
|
|
|
512
512
|
const { startDialerApp } = await import("./dialer/appServer.js");
|
|
513
513
|
const fingerprint = createHash("sha256").update(config.baseURL).update("\0").update(config.apiKey).digest("hex");
|
|
514
514
|
const sdk = new PyAI({ apiKey: config.apiKey, baseURL: config.baseURL, fetch: (url, init) => fetch(url, { ...init, redirect: "error", signal: init?.signal ?? AbortSignal.timeout(15000) }) });
|
|
515
|
-
const app = await startDialerApp({ sdk, userId: `desktop_${fingerprint.slice(0, 40)}`, scope: `desktop:${fingerprint}`, port, name: value(flags, "name"), whiteLabel: flags["white-label"] === true });
|
|
515
|
+
const app = await startDialerApp({ sdk, userId: `desktop_${fingerprint.slice(0, 40)}`, scope: `desktop:${fingerprint}`, port, name: value(flags, "name"), whiteLabel: flags["white-label"] === true, allowTransfers: flags.transfers === true });
|
|
516
516
|
output({ url: app.url, local_only: true }, flags, `PyAI Phone is ready at ${app.url}\nKeep this window open. Press Ctrl+C to stop the app and end its owned calls.`);
|
|
517
517
|
if (flags["no-browser"] || !(await openBrowser(app.launchUrl)))
|
|
518
518
|
process.stderr.write(`Private one-use launch link (valid for 60 seconds): ${app.launchUrl}\n`);
|
|
@@ -6,6 +6,8 @@ export interface DialerAppOptions {
|
|
|
6
6
|
scope: string;
|
|
7
7
|
name?: string;
|
|
8
8
|
whiteLabel?: boolean;
|
|
9
|
+
/** Operator opt-in; the API independently enforces transfer activation. */
|
|
10
|
+
allowTransfers?: boolean;
|
|
9
11
|
port?: number;
|
|
10
12
|
/** Explicit asset injection for packaging/tests, never selected by HTTP. */
|
|
11
13
|
assets?: {
|
package/dist/dialer/appServer.js
CHANGED
|
@@ -7,6 +7,7 @@ import { createRequire } from "node:module";
|
|
|
7
7
|
import { dirname, join } from "node:path";
|
|
8
8
|
import { Readable } from "node:stream";
|
|
9
9
|
import { createServerDialerBroker } from "./serverBroker.js";
|
|
10
|
+
import { dialerCallHandedOff } from "./managedDialer.js";
|
|
10
11
|
const terminal = (call) => ["completed", "failed", "canceled"].includes(call.status);
|
|
11
12
|
const secretMatches = (input, expected) => typeof input === "string" && /^[A-Za-z0-9_-]+$/.test(input) && input.length === expected.length && timingSafeEqual(Buffer.from(input), Buffer.from(expected));
|
|
12
13
|
const browserModules = new Set(["managedDialer", "dialingQueue", "indexedDbJournal", "voiceSdkAudio", "dialerElement", "meetingRecorder", "meetingRecordingStore", "meetingRecorderElement"]);
|
|
@@ -61,7 +62,7 @@ export async function startDialerApp(options) {
|
|
|
61
62
|
let bootstrapUsed = false;
|
|
62
63
|
let origin = "", closing = false;
|
|
63
64
|
const calls = new Set();
|
|
64
|
-
const remember = (call) => { if (terminal(call))
|
|
65
|
+
const remember = (call) => { if (terminal(call) || dialerCallHandedOff(call))
|
|
65
66
|
calls.delete(call.id);
|
|
66
67
|
else
|
|
67
68
|
calls.add(call.id); return call; };
|
|
@@ -99,19 +100,35 @@ export async function startDialerApp(options) {
|
|
|
99
100
|
}
|
|
100
101
|
if (url.pathname === "/api/config" && method === "GET") {
|
|
101
102
|
const owned = await numbers();
|
|
102
|
-
return json(response, 200, { name: options.name ?? (options.whiteLabel ? "Phone" : "PyAI Phone"), white_label: options.whiteLabel === true, scope: options.scope, user_id: options.userId, nonce,
|
|
103
|
+
return json(response, 200, { name: options.name ?? (options.whiteLabel ? "Phone" : "PyAI Phone"), white_label: options.whiteLabel === true, transfers_enabled: options.allowTransfers === true, scope: options.scope, user_id: options.userId, nonce,
|
|
103
104
|
numbers: owned.map(number => ({ id: number.id, phone_number: number.phone_number, country: number.country })) });
|
|
104
105
|
}
|
|
105
106
|
if (url.pathname === "/api/sessions" && method === "POST") {
|
|
106
107
|
if (closing)
|
|
107
108
|
throw new AppError(503, "Dialer is closing. No new calls are admitted.");
|
|
108
109
|
const input = await body(request), key = request.headers["idempotency-key"];
|
|
110
|
+
if (input.transfer_enabled === true && options.allowTransfers !== true)
|
|
111
|
+
throw new AppError(400, "Open this dialer with transfers enabled before starting a transferable call.");
|
|
109
112
|
if (typeof key !== "string" || typeof input.from_number_id !== "string" || !(await numbers()).some(number => number.id === input.from_number_id))
|
|
110
113
|
throw new AppError(400, "Choose an owned voice number and a calling retry key.");
|
|
111
114
|
const session = await broker.create(input, key);
|
|
112
115
|
remember(session.call);
|
|
113
116
|
return json(response, 200, session);
|
|
114
117
|
}
|
|
118
|
+
const transferPath = /^\/api\/calls\/([A-Za-z0-9_-]{1,200})\/transfers(?:\/([A-Za-z0-9_-]{1,200})\/(complete|cancel))?$/.exec(url.pathname);
|
|
119
|
+
if (transferPath && method === "POST") {
|
|
120
|
+
if (closing || options.allowTransfers !== true)
|
|
121
|
+
throw new AppError(503, "Transfers are not enabled in this dialer.");
|
|
122
|
+
const id = transferPath[1], transferId = transferPath[2], operation = transferPath[3];
|
|
123
|
+
await own(id);
|
|
124
|
+
const input = await body(request);
|
|
125
|
+
if (transferId && (operation === "complete" || operation === "cancel"))
|
|
126
|
+
return json(response, 202, remember(await broker.transfers[operation](id, transferId)));
|
|
127
|
+
const key = request.headers["idempotency-key"];
|
|
128
|
+
if (typeof key !== "string" || typeof input.to !== "string" || !/^\+[1-9]\d{6,14}$/.test(input.to) || !["warm", "cold"].includes(String(input.mode)))
|
|
129
|
+
throw new AppError(400, "Choose a transfer destination, mode and retry key.");
|
|
130
|
+
return json(response, 202, remember(await broker.transfers.create(id, { to: input.to, mode: input.mode }, key)));
|
|
131
|
+
}
|
|
115
132
|
const callPath = /^\/api\/calls\/([A-Za-z0-9_-]{1,200})(?:\/(hangup|recording))?$/.exec(url.pathname);
|
|
116
133
|
if (callPath) {
|
|
117
134
|
const id = callPath[1], operation = callPath[2];
|
|
@@ -15,13 +15,17 @@ export class PyaiDialerElement extends HTMLElement {
|
|
|
15
15
|
header{display:flex;align-items:center;justify-content:space-between;gap:12px}h2{margin:0;font-size:16px;font-weight:650}.badge{font-size:11px;color:#667085;background:#f3f4f8;padding:4px 9px;border-radius:20px}
|
|
16
16
|
.status{display:flex;align-items:center;gap:7px;color:#667085;margin:20px 0 5px}.dot{width:7px;height:7px;border-radius:50%;background:#9a9dad}.dot.active{background:#12a777}
|
|
17
17
|
input{width:100%;border:0;border-bottom:1px solid #ececf2;padding:12px 0;font:500 26px/1.3 system-ui;letter-spacing:.5px;color:inherit;background:transparent}input:focus{outline:0;border-color:var(--dialer-accent)}
|
|
18
|
-
.hint{font-size:12px;color:#8c91a2;margin-top:8px}.keys{display:grid;grid-template-columns:repeat(3,1fr);gap:8px;margin:22px 0}.key{background:#f8f8fb;border:0;border-radius:14px;padding:9px;cursor:pointer;font:500 23px system-ui;color:inherit;min-height:57px}.key small{display:block;font-size:9px;letter-spacing:2px;color:#9296a5;margin-top:1px}.key:hover{background:#eeedf9}
|
|
18
|
+
.hint,.transfer-help{font-size:12px;color:#8c91a2;margin-top:8px}.keys{display:grid;grid-template-columns:repeat(3,1fr);gap:8px;margin:22px 0}.key{background:#f8f8fb;border:0;border-radius:14px;padding:9px;cursor:pointer;font:500 23px system-ui;color:inherit;min-height:57px}.key small{display:block;font-size:9px;letter-spacing:2px;color:#9296a5;margin-top:1px}.key:hover{background:#eeedf9}
|
|
19
19
|
.actions{display:flex;gap:8px}button{font:600 13px system-ui;cursor:pointer}button:focus-visible{outline:3px solid #bdb8ff;outline-offset:2px}.primary,.secondary{border:0;border-radius:13px;padding:14px 18px;min-height:46px}.primary{flex:1;color:white;background:var(--dialer-accent)}.primary.end{background:#da4055}.secondary{background:#f1f2f7;color:#535b70}button:disabled{opacity:.45;cursor:default}.error{color:#b42338;font-size:12px;min-height:18px;margin:12px 0 0}.privacy{font-size:11px;color:#8b91a1;margin:16px 0 0;text-align:center}
|
|
20
20
|
@media(prefers-color-scheme:dark){:host{color:#eaebf3}section{background:#191b26;border-color:#303341}.key{background:#252836}.key:hover,.secondary{background:#34364a}.badge{background:#282b39;color:#a7aec0}input{border-color:#36394a}.error{color:#ff9eaa}}
|
|
21
21
|
</style><section aria-label="Phone"><header><h2>Phone</h2><span class="badge">PyAI</span></header>
|
|
22
22
|
<div class="status" role="status" aria-live="polite"><span class="dot"></span><span data-status>Connect your workspace</span></div>
|
|
23
23
|
<input type="tel" inputmode="tel" autocomplete="tel" aria-label="Phone number" placeholder="+1 415 555 0100" maxlength="32"><div class="hint">Include the country code</div>
|
|
24
24
|
<div class="keys"></div><div class="actions"><button class="secondary" data-action="mute" disabled>Mute</button><button class="primary" data-action="call" disabled>Call</button><button class="secondary" data-action="recover" hidden>Recover</button></div>
|
|
25
|
+
<div data-transfer hidden><p data-transfer-status role="status" aria-live="polite"></p><input class="transfer-number" type="tel" aria-label="Transfer phone number" placeholder="+61 2 5550 1000" maxlength="32">
|
|
26
|
+
<div class="actions"><button class="secondary" data-action="warm">Consult first</button><button class="secondary" data-action="cold">Transfer now</button></div>
|
|
27
|
+
<p class="transfer-help">Consult first keeps your caller on hold while you talk to the target. Transfer now leaves the call before the target answers.</p>
|
|
28
|
+
<div class="actions"><button class="primary" data-action="complete-transfer" hidden>Complete transfer</button><button class="secondary" data-action="cancel-transfer" hidden>Return to caller</button><button class="secondary" data-action="recover-transfer" hidden>Recover transfer</button></div></div>
|
|
25
29
|
<p class="error" role="alert"></p><p class="privacy">Calls and recordings stay in your workspace</p></section>`;
|
|
26
30
|
const letters = ["", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ", "", "+", ""];
|
|
27
31
|
"123456789*0#".split("").forEach((digit, i) => {
|
|
@@ -50,6 +54,11 @@ export class PyaiDialerElement extends HTMLElement {
|
|
|
50
54
|
this.root.querySelector("[data-action=recover]").addEventListener("click", () => void this.run(() => this.dialer.retrySetup()));
|
|
51
55
|
this.root.querySelector("input").addEventListener("keydown", event => { if (event.key === "Enter")
|
|
52
56
|
void this.act(); });
|
|
57
|
+
for (const [action, mode] of [["warm", "warm"], ["cold", "cold"]])
|
|
58
|
+
this.root.querySelector(`[data-action=${action}]`).addEventListener("click", () => void this.run(() => this.dialer.transfer(this.root.querySelector(".transfer-number").value.replace(/[\s()-]/g, ""), mode)));
|
|
59
|
+
this.root.querySelector("[data-action=complete-transfer]").addEventListener("click", () => void this.run(() => this.dialer.completeTransfer()));
|
|
60
|
+
this.root.querySelector("[data-action=cancel-transfer]").addEventListener("click", () => void this.run(() => this.dialer.cancelTransfer()));
|
|
61
|
+
this.root.querySelector("[data-action=recover-transfer]").addEventListener("click", () => void this.run(() => this.dialer.retryTransfer()));
|
|
53
62
|
}
|
|
54
63
|
bind(dialer, inputFor, appearance = {}, finishWrapup) {
|
|
55
64
|
if (this.dialer && this.dialer !== dialer && this.dialer.current.state !== "idle")
|
|
@@ -68,20 +77,41 @@ export class PyaiDialerElement extends HTMLElement {
|
|
|
68
77
|
this.unsubscribe = this.dialer.subscribe(s => this.render(s)); }
|
|
69
78
|
disconnectedCallback() { this.unsubscribe?.(); this.unsubscribe = null; }
|
|
70
79
|
render(snapshot) {
|
|
71
|
-
const labels = { idle: "Ready to call", preparing: "Connecting…", ringing: "Ringing…", active: "Connected", stopping: "Confirming call ended…", unconfirmed: "Call needs recovery", wrapup: "Call finished" };
|
|
80
|
+
const labels = { idle: "Ready to call", preparing: "Connecting…", ringing: "Ringing…", active: "Connected", stopping: "Confirming call ended…", unconfirmed: "Call needs recovery", handing_off: "Transfer in progress · caller stays connected", transferred: "Transfer complete · caller stays connected", wrapup: "Call finished" };
|
|
72
81
|
this.root.querySelector("[data-status]").textContent = labels[snapshot.state];
|
|
73
82
|
this.root.querySelector(".dot").classList.toggle("active", snapshot.state === "active");
|
|
74
|
-
this.root.querySelector("input")
|
|
83
|
+
const destinationInput = this.root.querySelector("input");
|
|
84
|
+
destinationInput.disabled = snapshot.state !== "idle";
|
|
85
|
+
const group = snapshot.call?.dialing_mode === "parallel" ? snapshot.call : null;
|
|
86
|
+
if (snapshot.state !== "idle" && snapshot.call)
|
|
87
|
+
destinationInput.value = group
|
|
88
|
+
? group.connected_to ?? `${group.destinations?.length ?? 0} parallel lines` : snapshot.call.to;
|
|
89
|
+
this.root.querySelector(".hint").textContent = group
|
|
90
|
+
? group.lines?.map(line => `${line.to} · ${line.status}`).join(" / ") ?? "The first answered line connects. Other lines are canceled."
|
|
91
|
+
: "Include the country code";
|
|
75
92
|
const call = this.root.querySelector("[data-action=call]");
|
|
76
|
-
call.disabled = snapshot.state
|
|
77
|
-
call.classList.toggle("end", !["idle", "wrapup"].includes(snapshot.state));
|
|
78
|
-
call.textContent = snapshot.state === "idle" ? "Call" : snapshot.state === "
|
|
93
|
+
call.disabled = ["stopping", "handing_off"].includes(snapshot.state);
|
|
94
|
+
call.classList.toggle("end", !["idle", "wrapup", "transferred"].includes(snapshot.state));
|
|
95
|
+
call.textContent = snapshot.state === "idle" ? "Call" : ["wrapup", "transferred"].includes(snapshot.state) ? "Done" : snapshot.state === "handing_off" ? "Transferring…" : "End call";
|
|
79
96
|
const mute = this.root.querySelector("[data-action=mute]");
|
|
80
97
|
mute.disabled = snapshot.state !== "active";
|
|
81
98
|
mute.textContent = snapshot.muted ? "Unmute" : "Mute";
|
|
82
99
|
mute.setAttribute("aria-pressed", String(snapshot.muted));
|
|
83
100
|
this.root.querySelector("[data-action=recover]").hidden = !["unconfirmed", "stopping"].includes(snapshot.state);
|
|
84
101
|
this.root.querySelector(".error").textContent = snapshot.error ?? "";
|
|
102
|
+
const transfer = snapshot.call?.transfers?.at(-1), pending = transfer && !["completed", "canceled", "failed"].includes(transfer.status);
|
|
103
|
+
this.root.querySelector("[data-transfer]").hidden = snapshot.call?.transfer_enabled !== true || ["idle", "preparing", "ringing", "wrapup"].includes(snapshot.state);
|
|
104
|
+
this.root.querySelector("[data-transfer-status]").textContent = transfer ? `${transfer.to ?? "Transfer target"} · ${transfer.status}` : "Transfer this caller";
|
|
105
|
+
this.root.querySelector(".transfer-number").disabled = Boolean(pending || snapshot.transferPending || snapshot.state !== "active");
|
|
106
|
+
for (const action of ["warm", "cold"])
|
|
107
|
+
this.root.querySelector(`[data-action=${action}]`).disabled = Boolean(pending || snapshot.transferPending || snapshot.state !== "active");
|
|
108
|
+
const complete = this.root.querySelector("[data-action=complete-transfer]");
|
|
109
|
+
complete.hidden = transfer?.mode !== "warm" || transfer.status !== "ready";
|
|
110
|
+
complete.disabled = Boolean(snapshot.transferPending);
|
|
111
|
+
const cancel = this.root.querySelector("[data-action=cancel-transfer]");
|
|
112
|
+
cancel.hidden = transfer?.mode !== "warm" || !pending;
|
|
113
|
+
cancel.disabled = Boolean(snapshot.transferPending || ["completing", "canceling"].includes(transfer?.status ?? ""));
|
|
114
|
+
this.root.querySelector("[data-action=recover-transfer]").hidden = !snapshot.transferPending;
|
|
85
115
|
}
|
|
86
116
|
async run(action) {
|
|
87
117
|
try {
|
|
@@ -95,7 +125,7 @@ export class PyaiDialerElement extends HTMLElement {
|
|
|
95
125
|
if (!this.dialer || !this.inputFor)
|
|
96
126
|
return;
|
|
97
127
|
await this.run(async () => {
|
|
98
|
-
if (this.dialer.current.state
|
|
128
|
+
if (["wrapup", "transferred"].includes(this.dialer.current.state))
|
|
99
129
|
await this.wrapup();
|
|
100
130
|
else if (this.dialer.current.state !== "idle")
|
|
101
131
|
await this.dialer.stop();
|
|
@@ -5,7 +5,7 @@ export interface DialingContact {
|
|
|
5
5
|
input: TelephonyBrowserSessionCreate;
|
|
6
6
|
priority?: number;
|
|
7
7
|
}
|
|
8
|
-
/**
|
|
8
|
+
/** Auto, priority-based dynamic, and server-owned parallel dialing. A session must
|
|
9
9
|
* be explicitly started in the foreground; no calls resume automatically after
|
|
10
10
|
* reload. The backend rechecks policy and capacity for every admission. */
|
|
11
11
|
export declare class DialingQueue {
|
|
@@ -13,6 +13,8 @@ export declare class DialingQueue {
|
|
|
13
13
|
private running;
|
|
14
14
|
private advancing;
|
|
15
15
|
private activeId;
|
|
16
|
+
private activeIds;
|
|
17
|
+
private parallelLines;
|
|
16
18
|
private readonly dialer;
|
|
17
19
|
private readonly eligible;
|
|
18
20
|
constructor(dialer: ManagedDialer, eligible: (contact: DialingContact) => Promise<boolean>);
|
|
@@ -20,8 +22,11 @@ export declare class DialingQueue {
|
|
|
20
22
|
running: boolean;
|
|
21
23
|
remaining: number;
|
|
22
24
|
activeId: string | null;
|
|
25
|
+
activeIds: string[];
|
|
23
26
|
};
|
|
24
|
-
start(contacts: DialingContact[], mode?: "auto" | "dynamic"
|
|
27
|
+
start(contacts: DialingContact[], mode?: "auto" | "dynamic" | "parallel", options?: {
|
|
28
|
+
parallelLines?: number;
|
|
29
|
+
}): Promise<void>;
|
|
25
30
|
pause(): void;
|
|
26
31
|
resume(): Promise<void>;
|
|
27
32
|
/** Explicit wrap-up acknowledgment gates the next admission. A local audio
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** Auto, priority-based dynamic, and server-owned parallel dialing. A session must
|
|
2
2
|
* be explicitly started in the foreground; no calls resume automatically after
|
|
3
3
|
* reload. The backend rechecks policy and capacity for every admission. */
|
|
4
4
|
export class DialingQueue {
|
|
@@ -6,21 +6,37 @@ export class DialingQueue {
|
|
|
6
6
|
running = false;
|
|
7
7
|
advancing = false;
|
|
8
8
|
activeId = null;
|
|
9
|
+
activeIds = [];
|
|
10
|
+
parallelLines = 1;
|
|
9
11
|
dialer;
|
|
10
12
|
eligible;
|
|
11
13
|
constructor(dialer, eligible) {
|
|
12
14
|
this.dialer = dialer;
|
|
13
15
|
this.eligible = eligible;
|
|
14
16
|
}
|
|
15
|
-
get current() { return { running: this.running, remaining: this.contacts.length, activeId: this.activeId }; }
|
|
16
|
-
async start(contacts, mode = "auto") {
|
|
17
|
+
get current() { return { running: this.running, remaining: this.contacts.length, activeId: this.activeId, activeIds: [...this.activeIds] }; }
|
|
18
|
+
async start(contacts, mode = "auto", options = {}) {
|
|
17
19
|
if (this.running || this.advancing || this.dialer.current.state !== "idle")
|
|
18
20
|
throw new Error("Finish the current call before starting a queue.");
|
|
19
21
|
if (!contacts.length || contacts.length > 500 || new Set(contacts.map(c => c.id)).size !== contacts.length ||
|
|
20
|
-
contacts.some(c => !c.id || !/^\+[1-9]\d{6,14}$/.test(c.input.to) || !Number.isFinite(c.priority ?? 0)))
|
|
22
|
+
contacts.some(c => !c.id || typeof c.input.to !== "string" || c.input.destinations !== undefined || !/^\+[1-9]\d{6,14}$/.test(c.input.to) || !Number.isFinite(c.priority ?? 0)))
|
|
21
23
|
throw new Error("Choose 1–500 unique contacts with valid destinations and priorities.");
|
|
24
|
+
if (!["auto", "dynamic", "parallel"].includes(mode))
|
|
25
|
+
throw new Error("Choose a supported calling order.");
|
|
26
|
+
const parallelLines = mode === "parallel" ? options.parallelLines ?? 2 : 1;
|
|
27
|
+
if (!Number.isInteger(parallelLines) || parallelLines < 1 || parallelLines > 4 || mode === "parallel" && parallelLines < 2)
|
|
28
|
+
throw new Error("Parallel dialing requires two to four lines.");
|
|
29
|
+
if (mode === "parallel") {
|
|
30
|
+
if (contacts.some(contact => contact.input.transfer_enabled === true))
|
|
31
|
+
throw new Error("Transfer-enabled calls use single-line dialing.");
|
|
32
|
+
const fingerprint = (contact) => JSON.stringify([contact.input.from_number_id, contact.input.user_id,
|
|
33
|
+
contact.input.recording, contact.input.use_case ?? "support", contact.input.max_duration_seconds ?? 3600]);
|
|
34
|
+
if (new Set(contacts.map(c => c.input.to)).size !== contacts.length || contacts.some(c => fingerprint(c) !== fingerprint(contacts[0])))
|
|
35
|
+
throw new Error("Parallel contacts must have distinct numbers and share the same user, source and calling settings.");
|
|
36
|
+
}
|
|
37
|
+
this.parallelLines = parallelLines;
|
|
22
38
|
this.contacts = structuredClone(contacts);
|
|
23
|
-
if (mode === "dynamic")
|
|
39
|
+
if (mode === "dynamic" || mode === "parallel")
|
|
24
40
|
this.contacts.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
|
|
25
41
|
this.running = true;
|
|
26
42
|
await this.advance();
|
|
@@ -40,6 +56,7 @@ export class DialingQueue {
|
|
|
40
56
|
async finishWrapup() {
|
|
41
57
|
await this.dialer.finishWrapup();
|
|
42
58
|
this.activeId = null;
|
|
59
|
+
this.activeIds = [];
|
|
43
60
|
if (this.running)
|
|
44
61
|
await this.advance();
|
|
45
62
|
}
|
|
@@ -48,19 +65,36 @@ export class DialingQueue {
|
|
|
48
65
|
return;
|
|
49
66
|
this.advancing = true;
|
|
50
67
|
try {
|
|
51
|
-
|
|
52
|
-
|
|
68
|
+
const batch = [];
|
|
69
|
+
// Select the entire batch before admitting one immutable group. Pausing
|
|
70
|
+
// during eligibility cannot launch a partial group or consume contacts.
|
|
71
|
+
const skipped = new Set();
|
|
72
|
+
for (const contact of this.contacts) {
|
|
73
|
+
if (!this.running || batch.length >= this.parallelLines)
|
|
74
|
+
break;
|
|
53
75
|
const permitted = await this.eligible(structuredClone(contact));
|
|
54
76
|
if (!this.running)
|
|
55
77
|
return;
|
|
56
|
-
if (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
78
|
+
if (permitted)
|
|
79
|
+
batch.push(contact);
|
|
80
|
+
else
|
|
81
|
+
skipped.add(contact.id);
|
|
82
|
+
}
|
|
83
|
+
if (!this.running)
|
|
84
|
+
return;
|
|
85
|
+
this.contacts = this.contacts.filter(contact => !skipped.has(contact.id));
|
|
86
|
+
if (batch.length) {
|
|
87
|
+
const first = batch[0];
|
|
88
|
+
this.activeId = first.id;
|
|
89
|
+
this.activeIds = batch.map(contact => contact.id);
|
|
90
|
+
const { to: _to, destinations: _destinations, transfer_enabled: _transferEnabled, ...common } = first.input;
|
|
91
|
+
const intent = batch.length === 1 ? first.input
|
|
92
|
+
: { ...common, destinations: batch.map(contact => contact.input.to) };
|
|
93
|
+
// ManagedDialer persists the group and one key before any request.
|
|
94
|
+
// Unconfirmed admission remains fenced; no individual line is retried.
|
|
95
|
+
await this.dialer.start(intent);
|
|
96
|
+
const admitted = new Set(this.activeIds);
|
|
97
|
+
this.contacts = this.contacts.filter(contact => !admitted.has(contact.id));
|
|
64
98
|
return;
|
|
65
99
|
}
|
|
66
100
|
if (!this.contacts.length)
|
|
@@ -40,6 +40,25 @@ export class IndexedDbDialerJournal {
|
|
|
40
40
|
}
|
|
41
41
|
if (prior?.stopping)
|
|
42
42
|
next.stopping = true;
|
|
43
|
+
next.retiredTransferKeys = [...new Set([...(prior?.retiredTransferKeys ?? []), ...(next.retiredTransferKeys ?? [])])];
|
|
44
|
+
if (next.transfer && next.retiredTransferKeys.includes(next.transfer.key)) {
|
|
45
|
+
fail("This transfer has already finished. Refresh the original call.");
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (prior?.transfer && !next.retiredTransferKeys.includes(prior.transfer.key)) {
|
|
49
|
+
if (!next.transfer)
|
|
50
|
+
next.transfer = structuredClone(prior.transfer);
|
|
51
|
+
else if (prior.transfer.key !== next.transfer.key || prior.transfer.to !== next.transfer.to || prior.transfer.mode !== next.transfer.mode ||
|
|
52
|
+
prior.transfer.id && next.transfer.id && prior.transfer.id !== next.transfer.id ||
|
|
53
|
+
prior.transfer.command && next.transfer.command && prior.transfer.command !== next.transfer.command) {
|
|
54
|
+
fail("Another transfer is saved for this call. Recover the original transfer.");
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
next.transfer.id ??= prior.transfer.id;
|
|
59
|
+
next.transfer.command ??= prior.transfer.command;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
43
62
|
store.put(next);
|
|
44
63
|
result(undefined);
|
|
45
64
|
};
|